package itcast.dao.impl;

import itcast.dao.LackDao;
import itcast.domain.Build;
import itcast.domain.Lack;
import itcast.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class LackDaoImpl implements LackDao {
    /**
     * 申明JdbcTemplate类,出入连接池
     */
    private JdbcTemplate template=new JdbcTemplate(JDBCUtils.getDataSource());
    //数据库中查找缺寝信息

    @Override
    public List<Lack> findLack() {

        //使用jdbc操作数据库
        //1.定义sql
        String sql=" SELECT lack_id,student_id,buildmanager_id, lack_Date,student_name,student_sex,student_class,drom_name,lack_remark,buildmanager_name FROM lack,student,drom,buildmanager WHERE lack.Lack_StudentID=student.Student_ID and lack.Lack_BuildmanageID=buildmanager.Buildmanager_ID AND student.Student_DomitoryID=drom.Drom_ID";
        //query返回list集合，BeanPropertyRowMapper指定字节码
        List<Lack> lacks=template.query(sql,new BeanPropertyRowMapper<Lack>(Lack.class));

        return lacks;
    }
    //在数据库中插入缺寝记录

    @Override
    public void addLack_drom(Lack lack) {
        //1.定义sql语句

        String sql3="select student_id from student where student_name=? ";
        int student_id=template.queryForObject(sql3,Integer.class,lack.getStudent_name());
        String sql4="select  buildmanager_id from buildmanager where buildmanager_name=?";
        int buildmanager_id=template.queryForObject(sql4,Integer.class,lack.getBuildmanager_name());
        String sql1="insert into lack(lack_studentid,lack_buildmanageid,lack_date,lack_remark) values(?,?,?,?)";
        //String sql2="update lack set lack_studentid=student_id where lack.lack_student =student.student_id and student_name="

        //2.执行sql
       // template.update(sql1,lack.getLack_studentid(),lack.getBuildmanager_id(),lack.getLack_date(),lack.getLack_remark());


        template.update(sql1,student_id,buildmanager_id,lack.getLack_date(),lack.getLack_remark());


    }
    //根据lack_id删除学生缺寝记录

    @Override
    public void deletelack_drom(int lack_id) {
        String sql="delete from lack where lack_id=?";
        template.update(sql,lack_id);
    }
}
